package com.spukmk3me.impl.libgdx;

import java.io.IOException;

import com.spukmk3me.math.Rectangle;
import com.spukmk3me.math.Rectanglef;
import com.spukmk3me.video.Font;

public class LibgdxFont extends Font
{
    @Override
    protected int _load() throws IOException
    {
        int rc = super._load();
        
        if ( rc == 0 )
            initTextureCoords();
        
        return rc;
    }
    
    Rectanglef getCharTexCoord( char c )
    {
        return m_texCoords[ getCharmap().indexOf( c ) ];
    }
    
    private void initTextureCoords()
    {
        m_texCoords = new Rectanglef[ getCharmap().length() ];
        
        int texWidth = getImage().width();
        int texHeight = getImage().height();
        
        for ( int i = 0; i != m_texCoords.length; ++i )
        {
            Rectanglef rect = new Rectanglef();
            Rectangle dim = getCharacterDimension( getCharmap().charAt( i ) );
            
            rect.x = (float)dim.x / texWidth; // u1 left
            rect.y = (float)(dim.y + dim.h) / texHeight; // v1 down
            rect.w = (float)(dim.x + dim.w) / texWidth; // u2 right
            rect.h = (float)dim.y / texHeight; // v2 top
            m_texCoords[ i ] = rect;
        }
    }
    
    Rectanglef[] m_texCoords;
}
